<script>
	import { isLoggedIn } from '@/utils/auth';

	export default {
		onLaunch() {
			console.log('App Launch');
			setTimeout(() => {
				this.guardLogin();
			}, 50);
		},
		onShow() {
			console.log('App Show');
			this.guardLogin();
		},
		onHide() {
			console.log('App Hide');
		},
		methods: {
			guardLogin() {
				if (isLoggedIn()) return;
				const pages = getCurrentPages();
				if (!pages || pages.length === 0) {
					// 页面栈还未准备好，稍后再检查，避免重复跳转导致闪烁
					setTimeout(() => {
						this.guardLogin();
					}, 100);
					return;
				}
				const currentRoute = pages[pages.length - 1]?.route || '';
				if (currentRoute === 'pages/login/login') return;
				uni.reLaunch({
					url: '/pages/login/login'
				});
			}
		}
	};
</script>

<style>
	/*每个页面公共css */
</style>
